package com.ems.mapper;

import com.ems.entity.MenuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ems.vo.MenuVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 菜单权限表 Mapper 接口
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@Mapper
public interface MenuMapper extends BaseMapper<MenuEntity> {


    @Select ( value = "<script>SELECT * FROM menu_info mi LEFT JOIN role_menu_info rmi ON mi.id=rmi.menuId " +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='menuVo.type!=null'> AND mi.type=#{menuVo.type} </if>" +
            "<if test='menuVo.roleId!=null'> AND rmi.roleId=#{menuVo.roleId} </if>" +
            "<if test='menuVo.parentId!=null'> AND mi.parentId=#{menuVo.parentId} </if>" +
            "</trim>ORDER BY mi.sort</script>")
    List<MenuVo> getList (@Param ( "menuVo" ) MenuVo menuVo );


    @Select ( value = "SELECT mi.*,cmi.menuName AS parentName\n" +
            "FROM menu_info mi\n" +
            "         LEFT JOIN menu_info cmi ON mi.parentId = cmi.id\n" +
            "ORDER BY mi.sort LIMIT #{menuVo.page}, #{menuVo.limit}")
    List<MenuVo> queryList(@Param ("menuVo") MenuVo menuVo);

    @Select ( value = "SELECT COUNT(1)\n" +
            "FROM menu_info mi\n" +
            "         LEFT JOIN menu_info cmi ON mi.parentId = cmi.id")
    Integer queryCount(@Param ("menuVo") MenuVo menuVo);

}
